const state = {
  sidebar: {
    opened: true,
    withoutAnimation: false
  },
  device: 'desktop',
  language: 'zh-CN',
  size: 'medium',
  loading: false,
  provinces: [],
  cities: [],
  aqiLevels: [
    { id: 1, name: '优', color: '#00e400', range: '0-50' },
    { id: 2, name: '良', color: '#ffff00', range: '51-100' },
    { id: 3, name: '轻度污染', color: '#ff7e00', range: '101-150' },
    { id: 4, name: '中度污染', color: '#ff0000', range: '151-200' },
    { id: 5, name: '重度污染', color: '#8f3f97', range: '201-300' },
    { id: 6, name: '严重污染', color: '#7e0023', range: '300+' }
  ]
}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_LANGUAGE: (state, language) => {
    state.language = language
  },
  SET_SIZE: (state, size) => {
    state.size = size
  },
  SET_LOADING: (state, loading) => {
    state.loading = loading
  },
  SET_PROVINCES: (state, provinces) => {
    state.provinces = provinces
  },
  SET_CITIES: (state, cities) => {
    state.cities = cities
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  setLanguage({ commit }, language) {
    commit('SET_LANGUAGE', language)
  },
  setSize({ commit }, size) {
    commit('SET_SIZE', size)
  },
  setLoading({ commit }, loading) {
    commit('SET_LOADING', loading)
  },
  loadProvinces({ commit }) {
    // 模拟省份数据，实际应从API获取
    const provinces = [
      { id: 1, name: '北京市' },
      { id: 2, name: '上海市' },
      { id: 3, name: '广东省' },
      { id: 4, name: '江苏省' },
      { id: 5, name: '浙江省' },
      { id: 6, name: '山东省' },
      { id: 7, name: '河南省' },
      { id: 8, name: '四川省' },
      { id: 9, name: '湖北省' },
      { id: 10, name: '湖南省' }
    ]
    commit('SET_PROVINCES', provinces)
  },
  loadCities({ commit }, provinceId) {
    // 模拟城市数据，实际应从API获取
    const citiesMap = {
      1: [{ id: 1, name: '北京市', provinceId: 1 }],
      2: [{ id: 2, name: '上海市', provinceId: 2 }],
      3: [
        { id: 3, name: '广州市', provinceId: 3 },
        { id: 4, name: '深圳市', provinceId: 3 },
        { id: 5, name: '珠海市', provinceId: 3 },
        { id: 6, name: '佛山市', provinceId: 3 }
      ],
      4: [
        { id: 7, name: '南京市', provinceId: 4 },
        { id: 8, name: '苏州市', provinceId: 4 },
        { id: 9, name: '无锡市', provinceId: 4 },
        { id: 10, name: '常州市', provinceId: 4 }
      ],
      5: [
        { id: 11, name: '杭州市', provinceId: 5 },
        { id: 12, name: '宁波市', provinceId: 5 },
        { id: 13, name: '温州市', provinceId: 5 },
        { id: 14, name: '嘉兴市', provinceId: 5 }
      ]
    }
    
    const cities = citiesMap[provinceId] || []
    commit('SET_CITIES', cities)
  }
}

const getters = {
  sidebar: state => state.sidebar,
  device: state => state.device,
  language: state => state.language,
  size: state => state.size,
  loading: state => state.loading,
  provinces: state => state.provinces,
  cities: state => state.cities,
  aqiLevels: state => state.aqiLevels,
  getAqiLevelById: state => id => {
    return state.aqiLevels.find(level => level.id === id)
  },
  getProvinceById: state => id => {
    return state.provinces.find(province => province.id === id)
  },
  getCityById: state => id => {
    return state.cities.find(city => city.id === id)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
} 